package in.muton.edushare.newentities.upgraded.singleInvite;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.newentities.UnivUserCreator;
import in.muton.edushare.newentities.upgraded.InviteUtils;
import in.muton.edushare.processing.objects.DeptDataHandler;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.UserDeptLinkDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Users;

public class DeptInvite implements SingleAudienceInvite {

	private static final long serialVersionUID = 5423076073245007001L;
	private IModel<Departments> deptModel;

	public DeptInvite(IModel<Departments> deptModel) {
		this.deptModel = deptModel;
	}

	public List<String> inviteUsers(int role, CayenneList<Users> userList,
			ORMInterface ormInterface) {
		List<String> statusList = new ArrayList<String>();
		List<IModel<Users>> users = userList.getObject();
		UserDeptLinkDAO udlDAO = new UserDeptLinkDAO(ormInterface);
		for (IModel<Users> u : users) {
			statusList.add(udlDAO
					.addUserFB(deptModel.getObject(), u.getObject(), role,
							EduShareSession.get().getLoggedInUser()));
		}
		return statusList;
	}

	public List<String> inviteUsers(int role,
			List<CayenneList<Users>> userList, ORMInterface ormInterface) {
		List<String> strList = new ArrayList<String>();
		for (CayenneList<Users> u : userList) {
			strList.addAll(inviteUsers(role, u, ormInterface));
		}
		return strList;
	}

	public List<String> inviteUsers(int role, String[] userList,
			ORMInterface ormInterface) {
		List<String> statusList = new ArrayList<String>();
		UserDeptLinkDAO udlDAO = new UserDeptLinkDAO(EduShareSession.get()
				.getSessionORMInterface());

		UnivUserCreator uuc = new UnivUserCreator(ormInterface,
				new DeptDataHandler(deptModel).getOwner(), InviteUtils
						.getMatchingUnivRole(role));

		for (String u : userList) {
			Users userVO = InviteUtils.getUserByString(u, ormInterface);
			if (userVO != null) {
				statusList.add(udlDAO.addUserFB(deptModel.getObject(), userVO,
						role, EduShareSession.get().getLoggedInUser()));

			} else {
				if (InviteUtils.isEmailId(u)) {
					uuc.addUsersToUniv(u);
					userVO = InviteUtils.getUserByString(u, ormInterface);
					if (userVO != null) {
						statusList.add(udlDAO.addUserFB(deptModel.getObject(),
								userVO, role, EduShareSession.get()
										.getLoggedInUser()));
					} else {
						statusList
								.add("Unable to invite user "
										+ u
										+ ". Please provide valid Edushare username or email ID");

					}
				} else {
					statusList
							.add("Unable to determine the type of user "
									+ u
									+ ". Please provide valid Edushare username or email ID");
				}
			}
		}
		return statusList;
	}

}
